import axios from 'axios';

/**
* URL GET 请求地址
* headers 请求头信息
* data 请求参数
*/
interface GetOptions {
    url:string;
    headers?:Record<string,string>;
    data?:Record<string,string|number>;
}

/**
* URL POST 请求地址
* headers 请求头信息
* data 请求body
* query GET 请求参数 (追加到URL后面的参数)
*/
interface PostOptions{
    url:string;
    headers?:Record<string,string>;
    data:any;
    query?:Record<string,string|number>;
}

const RequestUtils = {
    get<T>(options: GetOptions){
        const {url, data, headers} = options;
        return axios.get<T>(url, {params:data||{}, headers}).then(res=>res.data);
    },
    post<T>(options: PostOptions){
        const {url, data, headers, query} = options;
        return axios.post<T>(url, data, {headers, params:query}).then(res=>res.data);
    },
}

// 使用示例  
RequestUtils.get<{id:number;age:string;nick:string;}>({url:'',data:{name:'dd'}}).then(res=>{
    console.log(res.id);
    console.log(res.age);
    console.log(res.nick);
})



